<?php
// -----------------------------------------------------
// Created by : php-orm
// -----------------------------------------------------
// Date: 2021/5/28
// -----------------------------------------------------
// Time: 10:52
// -----------------------------------------------------
// User: tf <1096392101@qq.com>
// -----------------------------------------------------
declare (strict_types=1);

namespace Gao\orm\db;

use Gao\orm\db\PDOConnection;
use Gao\orm\db\Query;
use PDO;

class MysqlConnection extends PDOConnection
{
    protected $config;

    public function __construct()
    {
        $this->setConfig();
    }

    /**
     * 设置config
     */
    public function setConfig()
    {
        $this->config = [
            'type' => 'mysql',
            'hostname' => '127.0.0.1',
            'hostport' => 3306,
            'username'=>'root',
            'password'=>'root',
            'database'=>'tp_layui',
            // 连接dsn
            'dsn'             => '',
            // 数据库部署方式:0 集中式(单一服务器),1 分布式(主从服务器)
            'deploy'          => 0,
            // 是否需要断线重连
            'break_reconnect' => false,
            'prefix' => 'tp_',
        ];
    }

    /**
     * 获取连接
     * @return object
     */
    public function getConnect(): object
    {
        return $this->linkID;
    }

    use Formatter;
    /**
     * 取得数据表的字段信息
     * @access public
     * @param string $tableName
     * @return array
     * @throws \Exception
     */
    public function getFields(string $tableName): array
    {
        [$tableName] = explode(' ', $tableName);

        if (false === strpos($tableName, '`')) {
            if (strpos($tableName, '.')) {
                $tableName = str_replace('.', '`.`', $tableName);
            }
            $tableName = '`' . $tableName . '`';
        }

        $sql    = 'SHOW FULL COLUMNS FROM ' . $tableName;
        $pdo    = $this->getPDOStatement($sql);
        $result = $pdo->fetchAll(PDO::FETCH_ASSOC);
        $info   = [];

        if (!empty($result)) {
            foreach ($result as $key => $val) {
                $val = array_change_key_case($val);

                $info[$val['field']] = [
                    'name'    => $val['field'],
                    'type'    => $val['type'],
                    'notnull' => 'NO' == $val['null'],
                    'default' => $val['default'],
                    'primary' => strtolower($val['key']) == 'pri',
                    'autoinc' => strtolower($val['extra']) == 'auto_increment',
                    'comment' => $val['comment'],
                ];
            }
        }
        return $this->fieldCase($info);
    }

    /**
     * 解析pdo连接的dsn信息
     * @access protected
     * @param array $config 连接信息
     * @return string
     */
    protected function parseDsn(array $config): string
    {
        if (!empty($config['socket'])) {
            $dsn = 'mysql:unix_socket=' . $config['socket'];
        } elseif (!empty($config['hostport'])) {
            $dsn = 'mysql:host=' . $config['hostname'] . ';port=' . $config['hostport'];
        } else {
            $dsn = 'mysql:host=' . $config['hostname'];
        }
        $dsn .= ';dbname=' . $config['database'];

        if (!empty($config['charset'])) {
            $dsn .= ';charset=' . $config['charset'];
        }

        return $dsn;
    }

    /**
     * 返回数组
     * @param bool $procedure
     * @return array
     */
    public function getResult(bool $procedure = false): array
    {
        return parent::getResult($procedure);
    }

    /**
     * 获取前缀
     * @return mixed|string
     */
    public function getPrefix()
    {
        return $this->config['prefix'] ?? '';
    }

    /**
     * 获取执行条数
     * @return mixed
     */
    public function getRowCount()
    {
        return parent::getRowCount();
    }
}